package com.yun.zf.admin.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.zf.admin.mapper.ZfCommunitySubwayMapper;
import com.yun.zf.admin.service.ZfCommunitySubwayService;
import com.yun.zf.common.admin.dto.community.CommunitySubwayDto;
import com.yun.zf.common.app.domain.ZfCommunitySubway;
import com.yun.zf.system.service.ZfSubwayStationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 小区地铁关系表 服务实现类
 * </p>
 *
 * @author cj
 * @since 2021-03-22
 */
@Service
public class ZfCommunitySubwayServiceImpl extends ServiceImpl<ZfCommunitySubwayMapper, ZfCommunitySubway> implements ZfCommunitySubwayService {

    @Autowired
    private ZfSubwayStationService subwayStationService;

    /**
     * 获取地铁线，地铁站点下的小区id
     * @param subwayId
     * @param stationPoi
     * @return
     */
    @Override
    public List<Map<String, Object>> getCommunitys(Long subwayId, String stationPoi) {
        List<String> pois= new ArrayList<>();
        // 如果地铁站位空，则查找当前地铁线下的所有站点
        if (StringUtils.isEmpty(stationPoi)){
            pois =   subwayStationService.getStationPois(subwayId);
        }else {
           pois = Arrays.asList(stationPoi.split(",")).stream().map(s -> s.trim()).collect(Collectors.toList());
        }
        QueryWrapper<ZfCommunitySubway> communitySubwayQueryWrapper = new QueryWrapper<>();
        communitySubwayQueryWrapper.select("community_id").in("subway_station_id", pois);

        return baseMapper.selectMaps(communitySubwayQueryWrapper);
    }

    @Override
    public int addCommunitySubway(ZfCommunitySubway communitySubway) {
        return baseMapper.insert(communitySubway);
    }

    /**
     * 通过小区Id 查询小区附近地铁站信息
     * @param communityId
     * @return
     */
    @Override
    public List<String> getStationsById(Long communityId) {
        QueryWrapper<ZfCommunitySubway> communitySubwayQueryWrapper = new QueryWrapper<>();
        communitySubwayQueryWrapper.lambda().eq(ZfCommunitySubway::getCommunityId,communityId);
      List<ZfCommunitySubway> subways =  baseMapper.selectList(communitySubwayQueryWrapper);
      List<String> stations = new ArrayList<>();
      if (subways.size()>0){
          for (ZfCommunitySubway station: subways
               ) {
              stations.add(station.getStationPoi());
          }
      }
        return  stations;
    }

    @Override
    public List<CommunitySubwayDto> getSubwayList(Long communityId) {
        return baseMapper.getSubwayList(communityId);
    }
}
